﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ChiDao.Business.Service.Impl;
using ChiDao.Site.Models;
using Ext.Net.MVC;
using Ext.Net;
using ChiDao.Business.Models.FlowProcess;
using System.Xml;
using System.Web.Script.Serialization;
using ChiDao.Business.Service;
using ChiDao.Component.Utils;
using ChiDao.Business.Service.AttachAttribute;

namespace ChiDao.Site.Web.Controllers
{
    [Export]
    public class HolidayRequestController : BaseController
    {
        [Import]
        public IHolidayRequestService HolidayRequestService { get; set; }
        [Import]
        public IProcessUtilsService ProcessUtilsService { get; set; }

        [WebPage]
        public ActionResult HolidayRequest(string processID)
        {
            this.GetCmp<TextField>("txtRequestPerson").Disable();
            if (string.IsNullOrEmpty(processID))
            {
                this.GetCmp<Button>("btnCirculation").Disable();
                this.GetCmp<Button>("btnBack").Disable();
                this.GetCmp<Button>("btnCancel").Disable();

                HolidayRequestView holidayRequestView = new HolidayRequestView();
                holidayRequestView.ID = Guid.NewGuid();
                holidayRequestView.RequestPerson = User.Identity.Name;
                holidayRequestView.HandleState = "0";
                return View(holidayRequestView);
            }
            else
            {
                HolidayRequestView holidayRequestView = HolidayRequestService.GetHolidayRequestByID(processID);
                if (holidayRequestView.HandleState == "0")
                {
                    this.GetCmp<Button>("btnBack").Disable();
                    this.GetCmp<Button>("btnCancel").Disable();
                }
                else if (holidayRequestView.HandleState == "1" || holidayRequestView.HandleState == "4")
                {
                    this.GetCmp<TextArea>("taRequestReason").Disable();
                    this.GetCmp<DateField>("dfRequestStartDate").Disable();
                    this.GetCmp<DateField>("dfRequestEndDate").Disable();
                    this.GetCmp<Button>("btnSave").Disable();
                    if (holidayRequestView.RequestPerson == User.Identity.Name)
                    {
                        this.GetCmp<Button>("btnCirculation").Disable();
                        this.GetCmp<Button>("btnBack").Disable();
                        this.GetCmp<Button>("btnCancel").Disable();
                    }
                }
                else
                {
                    this.GetCmp<TextArea>("taRequestReason").Disable();
                    this.GetCmp<DateField>("dfRequestStartDate").Disable();
                    this.GetCmp<DateField>("dfRequestEndDate").Disable();
                    this.GetCmp<Button>("btnSave").Disable();
                    this.GetCmp<Button>("btnCirculation").Disable();
                    this.GetCmp<Button>("btnBack").Disable();
                    this.GetCmp<Button>("btnCancel").Disable();
                }
                return View(holidayRequestView);
            }
        }

        [DirectMethod]
        public ActionResult SaveHoliday(string id, string requestReason, DateTime? requestStartDate, DateTime? requestEndDate)
        {
            if (string.IsNullOrEmpty(requestReason) || requestStartDate == null || requestEndDate == null)
            {
                X.Msg.Confirm("确认", "申请原因、请假开始时间和请假结束时间不能为空！", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
            }
            else
            {
                DateTime dtStartDate = Convert.ToDateTime(requestStartDate);
                DateTime dtEndDate = Convert.ToDateTime(requestEndDate);
                if (dtEndDate.CompareTo(dtStartDate) < 0)
                {
                    X.Msg.Confirm("确认", "请假开始时间不能小于请假结束时间！", new MessageBoxButtonsConfig
                    {
                        Ok = new MessageBoxButtonConfig { Text = "Ok" }
                    }).Show();
                }
                else
                {
                    HolidayRequestService.SaveHoliday(Guid.Parse(id), User.Identity.Name, requestReason, dtStartDate, dtEndDate);
                    X.Msg.Confirm("确认", "保存成功！", new MessageBoxButtonsConfig
                    {
                        Ok = new MessageBoxButtonConfig { Handler = "saveHolidaySuccess()", Text = "Ok" }
                    }).Show();
                }
            }
            return this.Direct();
        }

        [DirectMethod]
        public ActionResult BackHoliday(string id)
        {
            try
            {
                string requestPerson = HolidayRequestService.GetHolidayRequestByID(id).RequestPerson;
                bool isRequestNode = ProcessUtilsService.BackRequest("HolidayRequest", id, requestPerson);
                string handleState = "4";
                if (isRequestNode)
                {
                    handleState = "0";
                }
                HolidayRequestService.UpdateHoliday(Guid.Parse(id), "", DateTime.Now, DateTime.Now, handleState, true);
                X.Msg.Confirm("确认", "回退成功！", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Handler = "backOrCancelHolidaySuccess()", Text = "Ok" }
                }).Show();
            }
            catch (Exception ex)
            {
                X.Msg.Confirm("确认", "回退失败！", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
            }
            return this.Direct();
        }

        [DirectMethod]
        public ActionResult CancelHoliday(string id)
        {
            try
            {
                ProcessUtilsService.CancelRequest("HolidayRequest", id);
                HolidayRequestService.UpdateHoliday(Guid.Parse(id), "", DateTime.Now, DateTime.Now, "3", true);
                X.Msg.Confirm("确认", "终止成功！", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Handler = "backOrCancelHolidaySuccess()", Text = "Ok" }
                }).Show();
            }
            catch (Exception ex)
            {
                X.Msg.Confirm("确认", "终止失败！", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
            }
            return this.Direct();
        }

        public ActionResult GenerateGraph(string processID)
        {
            HolidayRequestView holidayRequestView = HolidayRequestService.GetHolidayRequestByID(processID);
            string handleState = "";
            if (holidayRequestView != null)
            {
                handleState = holidayRequestView.HandleState;
            }
            WorkFlowNewView workFlowNewView = ProcessUtilsService.GenerateGraph("HolidayRequest", processID, handleState);
            string jsonStr = JSON.Serialize(workFlowNewView);
            return PartialView("PreviewGraphNew", jsonStr);
        }

        [WebPage]
        public ActionResult HolidayRequestList()
        {
            return View(HolidayRequestService.GetHolidayRequestByRequestPerson(User.Identity.Name));
        }

        [DirectMethod]
        public ActionResult PopHolidayRequest()
        {
            return new Ext.Net.MVC.PartialViewResult { ViewName = "PopHolidayRequest" };
        }

        [DirectMethod]
        public ActionResult PopCirculationHoliday()
        {
            return new Ext.Net.MVC.PartialViewResult { ViewName = "PopCirculationProcessNew" };
        }
    }
}